package com.hamilton.erik.pt.core.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hamilton.erik.pt.core.dao.EnclosureMaintenanceDAO;
import com.hamilton.erik.pt.core.db.H2JdbcAccess;
import com.hamilton.erik.pt.core.model.EnclosureMaintenance;

public class H2EnclosureMaintenanceDAO implements EnclosureMaintenanceDAO {

	private final String ENCLOSURE_MAINTENANCE_DB_TABLE = "APPLICATION.ENCLOSURE_MAINTENANCE";
	
	private final String INSERT_ENCLOSURE_MAINTENANCE =
			String.format(
				"INSERT INTO %s (ENCLOSURE_ID, ACTION, MAINTENANCE_TIME) " +
				"VALUES (?, ?, ?)",
				ENCLOSURE_MAINTENANCE_DB_TABLE);
	
	private final String UPDATE_ENCLOSURE_MAINTENANCE =
			String.format(
				"UPDATE %s SET ACTION=?, MAINTENANCE_TIME=? WHERE ID=?",
				ENCLOSURE_MAINTENANCE_DB_TABLE);
	
	private final String DELETE_ENCLOSURE_MAINTENANCE =
			String.format("DELETE FROM %s WHERE ID=?", ENCLOSURE_MAINTENANCE_DB_TABLE);
	
	private final String GET_ALL_ENCLOSURE_MAINTENANCE =
			String.format(
				"SELECT * FROM %s WHERE ENCLOSURE_ID = ?", ENCLOSURE_MAINTENANCE_DB_TABLE);
	
	private final String DELETE_ALL_ENCLOSURE_MAINTENANCE =
			String.format("DELETE FROM %s WHERE ENCLOSURE_ID=?", ENCLOSURE_MAINTENANCE_DB_TABLE);

	@Override
	public int insertEnclosureMaintenance(EnclosureMaintenance enclosureMaintenance) throws SQLException {

		int insertedRow = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(INSERT_ENCLOSURE_MAINTENANCE);
		
			stmt.setLong(1, enclosureMaintenance.getEnclosureId());
			stmt.setString(2, enclosureMaintenance.getAction().getAction());
			stmt.setString(3, enclosureMaintenance.getTimestamp());
			
			insertedRow = stmt.executeUpdate();
			
			conn.commit();
	
			return insertedRow;
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public boolean deleteEnclosureMaintenance(Long enclosureMaintenanceId) throws SQLException {

	int returnValue = -1;
	Connection conn = null;
	
	try {
		conn = H2JdbcAccess.getInstance().getConnection();
		conn.setAutoCommit(false);
					
		PreparedStatement stmt = conn.prepareStatement(DELETE_ENCLOSURE_MAINTENANCE);

		stmt.setLong(1, enclosureMaintenanceId);
		
		returnValue = stmt.executeUpdate();
		
		conn.commit();

		if(returnValue > 0) {
			return true;
		}
	} catch (SQLException e) {
		throw e;
	} finally {
		if(conn != null) try { conn.close(); } catch (Exception ignore) {}
	}
	
	return false;
}

	@Override
	public EnclosureMaintenance findEnclosureMaintenance(
			EnclosureMaintenance enclosureMaintenance) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateEnclosureMaintenance(EnclosureMaintenance enclosureMaintenance)
			throws SQLException {

		int updatedRow = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(UPDATE_ENCLOSURE_MAINTENANCE);
		
			stmt.setString(1, enclosureMaintenance.getAction().getAction());
			stmt.setString(2, enclosureMaintenance.getTimestamp());
			stmt.setLong(3, enclosureMaintenance.getId());
			
			updatedRow = stmt.executeUpdate();
			
			conn.commit();

			return updatedRow;
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public List<EnclosureMaintenance> getAllEnclosureMaintenance(Long enclosureId) throws SQLException {

		List<EnclosureMaintenance> enclosureMaintenance = new ArrayList<EnclosureMaintenance>();
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
						
			PreparedStatement stmt = conn.prepareStatement(GET_ALL_ENCLOSURE_MAINTENANCE);
		
			stmt.setLong(1, enclosureId);
			
			ResultSet rs = stmt.executeQuery();
			
			while(rs.next()) {
				EnclosureMaintenance newEnclosureMaintenance = new EnclosureMaintenance();
	
				newEnclosureMaintenance.setId(rs.getLong(1));
				newEnclosureMaintenance.setEnclosureId(rs.getLong(2));
				newEnclosureMaintenance.setAction(rs.getString(3));
				newEnclosureMaintenance.setTimestamp(rs.getString(4));
				
				enclosureMaintenance.add(newEnclosureMaintenance);
			}
			
			return enclosureMaintenance;
			
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
	}

	@Override
	public boolean deleteAllEnclosureMaintenance(Long enclosureId) throws SQLException {

		int returnValue = -1;
		Connection conn = null;
		
		try {
			conn = H2JdbcAccess.getInstance().getConnection();
			conn.setAutoCommit(false);
						
			PreparedStatement stmt = conn.prepareStatement(DELETE_ALL_ENCLOSURE_MAINTENANCE);

			stmt.setLong(1, enclosureId);
			
			returnValue = stmt.executeUpdate();
			
			conn.commit();

			if(returnValue > 0) {
				return true;
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			if(conn != null) try { conn.close(); } catch (Exception ignore) {}
		}
		
		return false;
	}
	
}
